package com.example.win.android_banduyujia.peopleactivity.activity.activity.openactivity.courseactivity.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.entity.CurrEntity;
import com.example.win.android_banduyujia.peopleactivity.activity.activity.openactivity.courseactivity.adapter.CourseAdapter;
import com.example.win.android_banduyujia.peopleactivity.activity.activity.openactivity.courseactivity.database.MycourseDatabase;
import com.example.win.android_banduyujia.peopleactivity.ui.activity.BaseTitleBarActivity;
import com.example.win.android_banduyujia.peopleactivity.ui.layout.RefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Win on 2015/9/6.
 */
public class CourseActivity extends BaseTitleBarActivity implements SwipeRefreshLayout.OnRefreshListener, RefreshLayout.OnLoadListener {
    public static final String TAG = CourseActivity.class.getSimpleName();

    private RefreshLayout  swipeLayout;
    private ArrayList<HashMap<String, String>>  list;
    private View  header;

    private RecyclerView      recyclerView;
    private CourseAdapter mAdapter;
    private List<CurrEntity>  mDataList;
    private MycourseDatabase mDatabase;

    private String  mAccessToken;
    private int     mCurrentPage;
    private int     mLastPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_course_page);

        mDatabase = MycourseDatabase.getInstance();

        InitTitles();
        InitViews();
        loadDatas();
    }

    private void InitTitles() {
        showLeftBackButton();
        setTitle(R.string.fragment_send_course);
    }

    private void loadDatas() {
        mDataList = mDatabase.getAllCourse();
        if(mDataList == null || mDataList.size() == 0){
            swipeLayout.setVisibility(View.VISIBLE);
        }else{
            swipeLayout.setVisibility(View.GONE);
            mAdapter = new CourseAdapter(this, mDataList);
            recyclerView.setAdapter(mAdapter);
        }
    }

    private void InitViews() {
        recyclerView = (RecyclerView) findViewById(R.id.recycler_course_list_vertical);
        LinearLayoutManager  layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        String[]  dataset = new String[3];
        for (int i=0;i>dataset.length;i++){
            dataset[3]="items"+i;
        }

        mAdapter = new CourseAdapter(this, mDataList);
        recyclerView.setAdapter(mAdapter);

        header = getLayoutInflater().inflate(R.layout.layout_refresh_header, null);
        swipeLayout=(RefreshLayout) findViewById(R.id.swipe_container);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

        list = new ArrayList<HashMap<String, String>>();
        for(int i=0; i <10;i++){
            list.add(new HashMap<String, String>());
        }
    }

    @Override
    public void onRefresh() {
        swipeLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshDatasFromServer(1);
                swipeLayout.setRefreshing(false);

                if(mAdapter == null){
                    mAdapter = new CourseAdapter(this, mDataList);
                    recyclerView.setAdapter(mAdapter);
                    swipeLayout.setVisibility(View.GONE);
                }else{
                    mAdapter.setListDatas(mDataList);
                }
            }
        }, 1000);
    }

    @Override
    public void onLoad() {
        swipeLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(mCurrentPage != mLastPage){
                    swipeLayout.setLoading(false);
                    refreshDatasFromServer(mCurrentPage+1);
                }
                if(mAdapter == null){
                    mAdapter = new CourseAdapter(this, mDataList);
                    recyclerView.setAdapter(mAdapter);
                    swipeLayout.setVisibility(View.GONE);
                }else{
                    mAdapter.setListDatas(mDataList);
                }
            }
        },1000);
    }

    private void refreshDatasFromServer(int i) {

    }
}
